package mamikeji.liaowa.ui.activity.right;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.squareup.okhttp.Request;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.EventDetailsBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

public class EventDetailsActivity extends BaseActivity implements View.OnClickListener{
    private WebView webView_event;
    private ProgressBar progressBar_event_details;

    private static final String TAG = "EventDetailsActivity";

    //分享弹框
    private Dialog dialog_share;

    private EventDetailsBean eventDetailsBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_details);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.event_details_name);
        setTitleRightBtn(R.string.event_details_right, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobclickAgent.onEvent(EventDetailsActivity.this,"ActivityShore");//活动-分享
                dialog_share.show();
            }
        });

        getDialogShare();

        eventDetailsBean = (EventDetailsBean) getIntent().getSerializableExtra("ActivityBean");

        webView_event = (WebView) findViewById(R.id.webView_event);
        progressBar_event_details = (ProgressBar) findViewById(R.id.progressBar_event_details);

        WebSettings webSettings = webView_event.getSettings();

        // 设置WebView属性，能够执行JavaScript脚本
        webSettings.setJavaScriptEnabled(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);

        webView_event.setHorizontalScrollBarEnabled(false);//禁止水平滚动
        webView_event.setVerticalScrollBarEnabled(true);//允许垂直滚动


        webView_event.loadUrl(eventDetailsBean.getJumpurl()+"?id="+eventDetailsBean.getId()+"&usercode="+ ForPreference.getString(ForContents.USER_CODE));

        webView_event.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressBar_event_details.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    progressBar_event_details.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar_event_details.setProgress(newProgress);//设置进度值
                }
            }

        });
    }

    /**
     * 分享弹框
     */
    public void getDialogShare() {
        dialog_share = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.diglog_set_shape, null);
        dialog_share.setContentView(view);
        Window dialogWindow = dialog_share.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;
        lp.width = getResources().getDisplayMetrics().widthPixels; // 宽度
        dialogWindow.setAttributes(lp);

        LinearLayout linearLayout_share_wechatFriend = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatFriend);
        LinearLayout linearLayout_share_wechatCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatCircle);
        LinearLayout linearLayout_share_tencentCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_tencentCircle);

        linearLayout_share_wechatFriend.setOnClickListener(this);
        linearLayout_share_wechatCircle.setOnClickListener(this);
        linearLayout_share_tencentCircle.setOnClickListener(this);
    }

    /**
     * 分享回调
     *
     * @param anEnum
     */
    private void ShareWeb(Enum anEnum) {
        UMImage thumb = new UMImage(this, eventDetailsBean.getImgurl());
        UMWeb web = new UMWeb(eventDetailsBean.getShareurl()+"?from=singlemessage");
        web.setThumb(thumb);
        web.setDescription(eventDetailsBean.getContent());
        web.setTitle(eventDetailsBean.getTitle());
        new ShareAction(this).withMedia(web).setPlatform((SHARE_MEDIA) anEnum).setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                ForToast.showToast("后台正在玩儿命加载~~~");
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                ForToast.showToast("分享成功！");
                getShareCallBack();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                ForLog.i("throwable", "" + throwable.toString());
                ForToast.showToast("分享失败！");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                ForToast.showToast("取消分享！");
            }
        }).share();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linearLayout_share_wechatFriend:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.linearLayout_share_wechatCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.linearLayout_share_tencentCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.QZONE);
                break;
        }
    }

    /**
     * 分享回调
     *
     * @return
     */
    public void getShareCallBack() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.SHARE_usercode, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.SHARE_aid, eventDetailsBean.getId()+"");
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.SHARE_CALLBACK, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG,e.getMessage());
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG,response);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView_event.onPause();
        MobclickAgent.onPageEnd("活动");
        MobclickAgent.onPause(this);       //结束统计时长
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView_event.onResume();
        MobclickAgent.onPageStart("活动");
        MobclickAgent.onResume(this);       //开始统计时长
    }

}
